package com.gsj.system.service.impl;

import com.gsj.common.pojo.Payload;
import com.gsj.common.pojo.R;
import com.gsj.common.utils.JwtUtils;
import com.gsj.system.config.RsaKeyProperties;
import com.gsj.system.eneity.Menu;
import com.gsj.system.eneity.User;
import com.gsj.system.mapper.MenuMapper;
import com.gsj.system.pojo.MenuVo;
import com.gsj.system.service.MenuService;
import com.gsj.system.utils.WebUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @program: clmseighth
 * @description:
 * @author: 张巍蓝
 * @create: 2021-07-19 16:39
 **/
@Service
public class MenuServiceImpl implements MenuService {
    @Resource
    private MenuMapper menuMapper;
    @Autowired
    private RsaKeyProperties rsaKeyProperties;
    @Override
    public R queryMenu() {
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        //2.解析该token得到用户对象
        Payload<User> infoFromToken = JwtUtils.getInfoFromToken(token, rsaKeyProperties.getPublicKey(), User.class);
        User userInfo = infoFromToken.getUserInfo();
        //3.根据用户id查询菜单列表
        List<Menu> menus=menuMapper.queryMenu(userInfo.getUserId());
        String userName = userInfo.getUserName();
        Integer userId = userInfo.getUserId();
        String avatar = userInfo.getAvatar();
        //4. 分级查询所有菜单
        //4.1 查询一级菜单
        List<Menu> firstMenu=new ArrayList<>();

        for(Menu menu:menus){
            int i = Integer.parseInt(menu.getVisible());
            if(menu.getParentId().equals(0) && i==0){

                firstMenu.add(menu);
            }
        }
        //4.2 查询一级菜单下所有的子菜单
        for(Menu first:firstMenu){
            first.setChildren(findChildren(menus,first.getMenuId()));
        }
        return R.ok().put("leftMenus",firstMenu).put("username",userName).put("id",userId).put("img",avatar);
    }

    @Override
    public R getMenuByRoleId(Integer roleId) {
        List<Menu> menus = menuMapper.getallMenu();
        List<Menu> menuByRoleId = menuMapper.getMenuByRoleId(roleId);
        List<Menu>  firstMenus=menus.stream().filter(item->{return  item.getParentId().equals(0);
        }).collect(Collectors.toList());
        //遍历一级菜单
        firstMenus.forEach(item->{
            item.setChildren(findChildren(menus,item.getMenuId()));
        });
        List<Integer> roleMenuIds=menuByRoleId.stream().map(item->{return  item.getMenuId();})
                .collect(Collectors.toList());
        return R.ok().put("menuByRoleId",roleMenuIds).put("menu",firstMenus);
    }

    @Override
    public R addMenu(MenuVo menuVo, String username) {
        menuMapper.addMenu(menuVo,username);
        return null;
    }

    @Override
    public R queryAllFatherId() {
        List<Menu> menus = menuMapper.queryAllFatherId();
        System.out.println(menus);
        return R.ok().put("menu",menus);
    }

    private List<Menu>  findChildren(List<Menu> menus,Integer pid){
        //声明一个集合容器存放孩子节点
        List<Menu> children=new ArrayList<>();
        //查询指定pid下的子菜单
        for(Menu child:menus){
            int i = Integer.parseInt(child.getVisible());
            if(child.getParentId().equals(pid) && i==0){
                children.add(child);
            }
        }
        //查询孩子节点下的孩子节点
        for(Menu c:children){
            c.setChildren(findChildren(menus,c.getMenuId()));
        }
        return children;

    }

}